package cq.zhcqk.activity;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.adapter.AXSTLeftAdapter;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.entity.AXSTBean;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.StringUtil;

/**
 * Created by zhangle on 2018/3/1.
 */
public class AXST_MainActivity extends BaseActivity implements View.OnClickListener{

    private Context context;
    private ListView list_1,list_2;
    private Map<String, Object> mapData;
    private List<AXSTBean> mapDatas;
    private AXSTLeftAdapter axstLeftAdapter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_axst_list);
        context = this;
        list_1 = (ListView) findViewById(R.id.list_1);
        list_2 = (ListView) findViewById(R.id.list_2);

        list_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //改变选中状态
                axstLeftAdapter.setCurrentItem(position);
                //通知ListView改变状态
                axstLeftAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initData() {
        getList();
        initBack();
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("爱心食堂");
        back.setOnClickListener(this);
    }

    private void getList(){
        RequestParams params = new RequestParams();
        String url = "";
        url = Common.URL+Common.DISHESLIST;
        buildBean.show();
        httpClient.post(url,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                if(StringUtil.stringToInt(map.get("code")+"") != 0){
                    Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
                    return;
                }
                mapData = (Map<String, Object>) map.get("data");
                mapDatas = new ArrayList<AXSTBean>();
                for (Map.Entry<String, Object> entry : mapData.entrySet()) {
                    mapDatas.add(new AXSTBean(entry.getKey(),entry.getValue()));
                    axstLeftAdapter = new AXSTLeftAdapter(context,mapDatas);
                    list_1.setAdapter(axstLeftAdapter);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("errorResponse："+errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                System.out.println("responseString："+responseString);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    @Override
    public void onClick(View v) {

    }
}
